import http from "../utils/request"
import {PageRequestType, ResponseType, PageResponseType} from "../types/Response";
import {AxiosResponse} from "axios";
import {GoodType} from "./GoodRequest";

const apiPrefix: string = "/cart";

export interface CartType {
    // 用户ID
    userId?: string | null | undefined
    // 主键
    id?: string | null | undefined
    // 商品ID
    goodId?: string | null | undefined
    // 商品ID
    good?: GoodType | null | undefined
    // 规格ID
    specificationId?: string | null | undefined
    // 数量
    number?: number | null | undefined
    // 创建时间
    create_at?: any | null | undefined
    // 修改时间
    update_at?: any | null | undefined
    // 稍后购买（1为不，2为是）
    laterBuy?: number | null | undefined
}

export interface GetCartRequestType {
    userId: string,
    cartList: CartType[],
    totalPrice: number,
    goodsPrice: number,
    freightPrice: number
}

export interface CartPageSearchType extends PageRequestType, CartType {
    current: 1;
    size: 10;
}


export async function addCart(data: {
    goodId: string,
    specificationId: string,
    number: number
}): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        data: data,
        url: apiPrefix,
        method: 'post'
    })
}

export async function deleteCartById(id: string): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        params: {
            id
        },
        url: apiPrefix + '/id',
        method: 'delete'
    })
}

export async function deleteCartByIds(ids: string[]): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        params: {
            ids
        },
        url: apiPrefix + '/batch',
        method: 'delete'
    })
}

export async function searchCart(): Promise<AxiosResponse<ResponseType<GetCartRequestType>>> {
    return await http.request({
        url: apiPrefix,
        method: 'get'
    })
}

export async function searchCartByPage(params: CartPageSearchType): Promise<AxiosResponse<ResponseType<PageResponseType<CartType>>>> {
    return await http.request({
        params: params,
        url: apiPrefix + '/page',
        method: 'get'
    })
}


export async function updateCartById(data: CartType): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        data: data,
        url: apiPrefix,
        method: 'put'
    })
}
